package com.example.cinemasystemv2.repository;

import com.example.cinemasystemv2.entity.SystemLog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface SystemLogRepository extends JpaRepository<SystemLog, Long> {
    
    List<SystemLog> findByUsername(String username);
    
    List<SystemLog> findByOperation(String operation);
    
    @Query("SELECT sl FROM SystemLog sl WHERE sl.logTime >= :startTime AND sl.logTime < :endTime ORDER BY sl.logTime DESC")
    List<SystemLog> findByDateRange(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
    
    @Query("SELECT sl FROM SystemLog sl WHERE DATE(sl.logTime) = DATE(:date) ORDER BY sl.logTime DESC")
    List<SystemLog> findByDate(@Param("date") LocalDateTime date);
    
    @Query("SELECT sl FROM SystemLog sl WHERE sl.logContent LIKE %:keyword% OR sl.username LIKE %:keyword% ORDER BY sl.logTime DESC")
    List<SystemLog> searchLogs(@Param("keyword") String keyword);
    
    @Query("SELECT sl FROM SystemLog sl ORDER BY sl.logTime DESC")
    List<SystemLog> findAllOrderByTimeDesc();
    
    @Query("SELECT COUNT(sl) FROM SystemLog sl WHERE DATE(sl.logTime) = DATE(:date)")
    long countByDate(@Param("date") LocalDateTime date);
}